using System;
using System.Xml;
using Scenario.WorkflowEngine.DataModel.WorkflowProcesses;
using Scenario.WorkflowEngine.DataModel.WorkflowRelevantData;
using Scenario.Modules.PCM.DataModel.ConfigurationManager.Entity;
using Scenario.Modules.PCM.DataModel.ConfigurationManager.Property;

namespace Scenario.Modules.FormHelper
{
	//**************************************************************************************
	// 
	// FormHelper Class
	//
	// The FormHelper class rappresents a method-set to offer help functions to forms from
	// Web applications that calls it to obtain execution of commands on processes or activities.
	// Forms can also obtains redirect url to switch their page cntrol after command execution.
	//
	//**************************************************************************************
	public class FormsHelper
	{
		private const string FORM_HELPER_CONFIG_FILE	= "FormHelper.xml";
		private const string FORM_HELPER_MAIN_TAG		= "ITPortal";
		private const string FORM_HELPER_MENU_TAG		= "Menu";

		//all
		private XMLFormActions xmlFormActions;

		//only ITPortal
		private XMLActions allowedActions;

		public FormsHelper()
		{
		}

		public void Setup()
		{
			WAPI.Response wapiResponse = new WAPI.Response();
			string sScenarioDB = WAPI.Session.GetScenarioDBDirectory (ref wapiResponse);

			string sFormActionsFile = sScenarioDB + "\\" + FORM_HELPER_CONFIG_FILE;
			xmlFormActions = new XMLFormActions();
	
			try
			{
				xmlFormActions.Load (sFormActionsFile);
				allowedActions = new XMLActions();
				allowedActions = xmlFormActions.Get(FORM_HELPER_MAIN_TAG);
			}
			catch ( bOSdotNET.Exception.bOSException err ) 
			{
				throw new Exception.FormHelperException(err.Message);
			}
		}

		public FormHelper GetFormHelper()
		{
			return new FormHelper(allowedActions);
		}

		public FormHelper GetFormHelperMenu()
		{
			XMLActions xmlActions = xmlFormActions.Get(FORM_HELPER_MENU_TAG);
			return new FormHelper(xmlActions);
		}
	}
}
